package main

import (
	"net/http"
	"fmt"
	"os"
	"io/ioutil"
	"regexp"
)

var (
	//手机号
	rePhone = `(1[3456789]\d)(\d{4})(\d{4})`

	/*邮箱*/
	//zhb1212@msn.com
	//reEmail = `[1-9]\d{4,}@qq\.com`
	//reEmail = `\w+@\w+\.com`
	reEmail = `[\w\.]+@\w+\.[a-z]{2,3}(\.[a-z]{2,3})?`

	/*超链接*/
	/*
	<a...href="https://www.hao123.com/link/https/?key=http%3A%2F%2Fwww.163.com%2F&amp;&amp;monkey=m-mingzhan-site&amp;c=CCBD97A87DB464FDCD39F475585D5AE6"...>
	*/
	reLink = `<a[\s\S]+?href="(http[\s\S]+?)"`

	/*身份证号:4-50121-1970-11-05-5756*/
	//reID = `[1-6]\d{5}-(	(19\d{2})	|	(20((0\d)|(1[0-8])))	)-((0[1-9])|(1[012]))-((0[1-9])|([12]\d)|(3[01]))-\d{3}[\dX]`
	reID = `[1-6]\d{5}((19\d{2})|(20((0\d)|(1[0-8]))))((0[1-9])|(1[012]))((0[1-9])|([12]\d)|(3[01]))\d{3}[\dX]`
)

func HandleErr(err error, when string) {
	if err != nil {
		fmt.Println(when, err)
		os.Exit(1)
	}
}

func spiderPhone() {
	html := GetHtml("https://www.haomagujia.com/")
	//fmt.Println(html)

	re := regexp.MustCompile(rePhone)
	allString := re.FindAllStringSubmatch(html, -1)
	for _, x := range allString {
		fmt.Println(x[0])
	}
}

func spiderEmail() {
	html := GetHtml("http://tieba.baidu.com/p/1634694280")
	html += "x.nimei@foxmail.com;"
	html += "nimei@itxdl.net;"
	html += "nimei666@21century.com.cn;"
	html += "nimei666@163.com;"

	re := regexp.MustCompile(reEmail)
	allString := re.FindAllStringSubmatch(html, -1)

	for _, x := range allString {
		fmt.Println(x[0])
	}
}

func spiderLink() {
	html := GetHtml("http://www.hao123.com")
	//fmt.Println(html)

	re := regexp.MustCompile(reLink)
	rets := re.FindAllStringSubmatch(html, -1)
	for _, x := range rets {
		fmt.Println(x[1])
	}
}

func main() {
	html := GetHtml("http://dy.163.com/v2/article/detail/D5D75EHH0514JN51.html")
	html+="123456201911084321"
	re := regexp.MustCompile(reID)
	rets := re.FindAllStringSubmatch(html, -1)
	for _, x := range rets {
		fmt.Println(x[0])
	}
}

func GetHtml(url string) string {
	resp, err := http.Get(url)
	HandleErr(err, `http.Get`)
	defer resp.Body.Close()

	bytes, _ := ioutil.ReadAll(resp.Body)
	html := string(bytes)

	return html
}
